/**
 * Created by zzy on 2017/10/26.
 */
/**
 * Created by zzy on 2017/9/26.
 */
import React, {Component} from 'react';
import {
    Text,
    View,
    TouchableOpacity,
    Image,
    StyleSheet,
    ScrollView,
    Dimensions,
    FlatList,
    PixelRatio,
    InteractionManager,
    RefreshControl,
    DeviceEventEmitter
} from 'react-native';
import ClassifyTab from '../componentluo/classifytab'
import DataUtil from '../util/DataUtil'
import NetRequestLuo from '../util/NetRequest'
import Icon from 'react-native-vector-icons/Ionicons'
import Toast from 'react-native-easy-toast'
import {observer} from 'mobx-react/native'
import CommonTitle from '../componentluo/CommonTitle'
import {observable, action, computed} from 'mobx'
import ProductCellH from '../componentluo/ProductCellH';
let {width,height} = Dimensions.get('window');
@observer
export default class Flagship extends Component{
    static navigationOptions = {
        header: ()=>{
            return  null
        }
    };
    @observable sortType='desc';
    @observable categoryType='saleMount';
    @observable sortSelectIndex=0;
    @observable typeData=[{title:'推荐',sort:false},{title:'销量',sort:false},{title:'价格',sort:true,sortType:true},{title:'品牌',sort:true,sortType:true}];
    constructor(props){
        super(props);
        this.state={
            loading:true,
            productlist:[],
            shopId:0
        };
    }
    render(){
        return <View style={{flex:1,backgroundColor:'#f5f5f5'}}>
            <CommonTitle title={'喜腾旗舰'} navigation={this.props.navigation}/>
            <ClassifyTab data={this.typeData} sortSelectIndex={this.sortSelectIndex} {...this.props} handler={(index)=>{
                this.fetch(index)
            }}/>
            <FlatList
                onRefresh={this._onRefresh.bind(this)}
                refreshing={this.state.loading}
                onEndReachedThreshold={0.1}
                onEndReached={this._onEndReached.bind(this)}
                data={this.state.productlist}
                numColumns={2}
                keyExtractor={(item,index) => index}
                renderItem={({item}) => <ProductCellH item={item} addToShoppingCart={this._addToShoppingCart.bind(this)} callback={()=>{
                    this.props.navigation.navigate('ProductsDetailsLuo',{tiket:item,shopId:this.state.shopId});
                }}/>}
            />
            <Toast ref="toast" position='bottom'/>
        </View>
    }
    componentDidMount(){
        InteractionManager.runAfterInteractions(() => {
            this.fetch();
        });
    }
    _onRefresh(){
        this.fetch();
    }
    _onEndReached(){

    }
    _addToShoppingCart(item){
        let param={
            productItemId: item.productItemModels[0].productItemId,
            productType: item.type,
            shopId: this.state.shopId,
        };
        NetRequestLuo.addShoppingCart(param,'increase').then((data)=>{
            LOG('加入购物车--------'+JSON.stringify(data));
            DeviceEventEmitter.emit('refreshShopList');//刷新购物车
            this.refs.toast.show('已加入购物车');
        })
    }
    fetch(index=0){
        this.sortSelectIndex=index;
        switch (index){
            case 0:
                this.sortType='desc';
                this.categoryType='saleMount';
                break;
            case 1:
                this.sortType='desc';
                this.categoryType='saleMount';
                break;
            case 2:
                this.typeData[index].sortType==true?this.sortType='desc':this.sortType='asc';
                this.categoryType='price';
                this.typeData[index].sortType=!this.typeData[index].sortType;
                break;
            case 3:
                this.typeData[index].sortType==true?this.sortType='desc':this.sortType='asc';
                this.categoryType='saleMount';
                this.typeData[index].sortType=!this.typeData[index].sortType;
                break;
        }
        let guessParam={
            sortType:this.sortType,
            categoryType:this.categoryType
        };
        NetRequestLuo.guessLike(guessParam).then((data)=>{
            LOG('猜你喜欢-------------'+JSON.stringify(data));
            if(data){
                this.setState({
                    loading:false,
                    productlist:data.productList,
                    shopId:data.shopId
                })
            }
            this.setState({
                loading:false
            })
        });
    }
}
